<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="网关名称" prop="gatewayId">
        <gateway-select @changeGateway="changeGateway"/>
      </el-form-item>

      <el-form-item label="报警类型" prop="warnType">
        <el-select v-model="queryParams.warnType"
                   placeholder="报警类型"
                   size="small" @change="changeWarnType">
          <el-option
            v-for="item in this.warnTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="报警时间">
        <el-date-picker
          v-model="time"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

<!--      <el-form-item label="${comment}" prop="projectId">-->
<!--        <el-input-->
<!--          v-model="queryParams.projectId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="${comment}" prop="gatewayId">-->
<!--        <el-input-->
<!--          v-model="queryParams.gatewayId"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="警告信息" prop="warnName">-->
<!--        <el-input-->
<!--          v-model="queryParams.warnName"-->
<!--          placeholder="请输入警告信息"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="警告级别" prop="warnLevel">-->
<!--        <el-input-->
<!--          v-model="queryParams.warnLevel"-->
<!--          placeholder="请输入警告级别"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="0未读 1已读" prop="isRead">-->
<!--        <el-input-->
<!--          v-model="queryParams.isRead"-->
<!--          placeholder="请输入0未读 1已读"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="0未处理 1已处理" prop="isDispose">-->
<!--        <el-input-->
<!--          v-model="queryParams.isDispose"-->
<!--          placeholder="请输入0未处理 1已处理"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:warnRecord:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:warnRecord:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['business:warnRecord:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:record:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="warnId" />-->
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="网关名称" align="center" prop="gatewayName" />
      <el-table-column label="报警类型" align="center" prop="warnType">
        <template slot-scope="scope">
          <span v-if="scope.row.warnType ==='3'">
            网关
          </span>
        </template>
      </el-table-column>
      <el-table-column label="报警信息" align="center" prop="warnName" />

      <el-table-column label="报警级别" align="center" prop="warnLevel">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.warnLevel ==='0'" type="danger" disable-transitions>
            0
          </el-tag>
          <el-tag v-if="scope.row.warnLevel ==='1'" type="success" disable-transitions>
            1
          </el-tag>
          <el-tag v-if="scope.row.warnLevel ==='2'" type="danger" disable-transitions>
            2
          </el-tag>
          <el-tag v-if="scope.row.warnLevel ==='3'" type="danger" disable-transitions>
            紧急
          </el-tag>
        </template>
      </el-table-column>

<!--      <el-table-column label="是否已读" align="center" prop="isRead">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag v-if="scope.row.isRead ==='0'" type="info" disable-transitions>-->
<!--            未读-->
<!--          </el-tag>-->
<!--          <el-tag v-if="scope.row.isRead ==='1'" type="success" disable-transitions>-->
<!--            已读-->
<!--          </el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->

<!--      <el-table-column label="是否处理" align="center" prop="isDispose">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag v-if="scope.row.isDispose ==='0'" type="info" disable-transitions>-->
<!--            未处理-->
<!--          </el-tag>-->
<!--          <el-tag v-if="scope.row.isDispose ==='1'" type="success" disable-transitions>-->
<!--            已处理-->
<!--          </el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="报警时间" align="center" prop="createTime" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['business:warnRecord:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['business:warnRecord:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报警信息记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="gatewayId">
          <el-input v-model="form.gatewayId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="警告信息" prop="warnName">
          <el-input v-model="form.warnName" placeholder="请输入警告信息" />
        </el-form-item>
        <el-form-item label="警告级别" prop="warnLevel">
          <el-input v-model="form.warnLevel" placeholder="请输入警告级别" />
        </el-form-item>
        <el-form-item label="0未读 1已读" prop="isRead">
          <el-input v-model="form.isRead" placeholder="请输入0未读 1已读" />
        </el-form-item>
        <el-form-item label="0未处理 1已处理" prop="isDispose">
          <el-input v-model="form.isDispose" placeholder="请输入0未处理 1已处理" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWarnRecord, getWarnRecord, delWarnRecord, addWarnRecord, updateWarnRecord } from "@/api/business/warnRecord"
import gatewaySelect from "../../common/GatewaySelect.vue"

export default {
  name: "Record",
  components: {
    gatewaySelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警信息记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        gatewayId: null,
        warnType: null,
        warnName: null,
        warnLevel: null,
        isRead: null,
        isDispose: null,
      },
      time: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      gatewayListOption: [],
      warnTypeList: [
        {value: '-1', label: '全部'},
        {value: '0', label: '回路'},
        {value: '1', label: '传感器'},
        {value: '2', label: '模块'},
        {value: '3', label: '网关'},
      ],
    }
  },
  created() {

  },
  methods: {

    changeWarnType() {
      this.getList()
    },

    // 接收子组件传递的消息
    changeGateway(obj) {
      this.gatewayListOption = obj.gatewayListOption
      this.queryParams.projectId = obj.projectId
      this.queryParams.gatewayId = obj.gatewayId
      this.getList()
    },

    getList() {
      this.loading = true
      listWarnRecord(this.queryParams).then(response => {
        this.recordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    cancel() {
      this.open = false
      this.reset()
    },

    reset() {
      this.form = {
        warnId: null,
        projectId: null,
        gatewayId: null,
        warnType: null,
        warnName: null,
        warnLevel: null,
        isRead: null,
        isDispose: null,
        createTime: null
      };
      this.resetForm("form")
    },

    handleQuery() {
      this.queryParams.pageNum = 1
      if (this.time.length > 0) {
        this.queryParams.beginTime = this.time[0]
        this.queryParams.endTime = this.time[1]
      }
      this.getList()
    },

    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },

    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warnId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加报警信息记录"
    },

    handleUpdate(row) {
      this.reset()
      const warnId = row.warnId || this.ids
      getWarnRecord(warnId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改报警信息记录"
      })
    },

    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warnId != null) {
            updateWarnRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addWarnRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    handleDelete(row) {
      const warnIds = row.warnId || this.ids
      this.$modal.confirm('是否确认删除报警信息记录编号为"' + warnIds + '"的数据项？').then(function() {
        return delWarnRecord(warnIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    handleExport() {
      this.download('system/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
